<?php
class FilesOperation {

	const UPLOAD_TRY = 9;

	/**
	 * Konstruktor.
	 */
	static $i = 0;
	static $i2 = 0;
	static $i3 = 0;
	public function __construct() {		
	}

	/**
	 * Uploaduje plik zgodnie z ustawieniami.
	 *
	 * @param <type> $file
	 * @param <type> $method
	 * @param <type> $conversion
	 * @return <type>
	 */
	public static function UploadFileToStorage($file, $method, $conversion = false, $localStorage = false) {

		if(STORAGE_TYPE == ST_TYPE_SERVER && $localStorage == false) {
			$logger = LoggerManager::getLogger(__CLASS__);

			try {
				$client = new WP_API_EdyskLib($file);
				$logger -> debug("File to send: " . $file);

				if ($conversion != false) {
					$client->SetConversion();
				}

				$response = $client->SendFile($file);

				$logger->debug($client->GetLastResponse());

				$outNew = WP_API_EdyskLib::GetFileNameFromResponse($response);

			} catch (ApiException $e) {
				$outNew = false;
			}

			if (self::$i == 0) {
				self::$i2++;
			}

			if (!$outNew)
			{
				if (self::$i <= self::UPLOAD_TRY) {
					//probojemy jeszcze ra
					self::$i++;
					$logger->debug("Bad e-dysk response: ");
					$logger->debug($client->GetLastResponse());
//					Utils::ArrayDisplay($client);
//					Utils::ArrayDisplay($client->GetLastResponse());
					$logger->debug("wgrywa jeszcze raz: ");
					self::UploadFileToStorage($file, $method, $conversion, $localStorage);
				}
				else
				{
					throw new ApiException("Can't upload file", 500);
				}
			} else {
				self::$i3 += self::$i;
				self::$i = 0;
				return $outNew;
			}
		} else {
			self::$i =0;
			return self::SaveFile($file, $method);
		}
	}

	/**
	 * Uploaduje plik na dysk lokalny serwera.
	 *
	 * @param <type> $file
	 * @param <type> $method
	 * @return <type>
	 */
	public static function SaveFile($file, $method) {
		$out = false;
		$fileName = end(explode(DIRECTORY_SEPARATOR, $file));
		$dirName = ereg_replace($fileName, "", $file);
		//echo "/opt/WP/bin/sync_file.sh ".$file." ".ereg_replace(PATH_ROOT.'/', '', $dirName);
//		$success = exec("/opt/WP/bin/sync_file.sh ".$file." ".ereg_replace(PATH_ROOT.'/', '', $dirName));
		$success = 0;
		if($success==0) {
			MFLog::info("Copy file: ".$fileName." to ".$dirName.", Method: ".$method.", Server reply: ".$success);
			$out = $fileName;
		} else {
			MFLog::warn("Copy file: ".$fileName." to ".$dirName.", Method: ".$method." FAILED!".", Server reply: ".$success);
			$array = array();
			$array = SessionProxy::GetValue('adminError');
			$array[] = $fileName.' nie zosta� przegrany na jeden lub wi�cej swerwer�w!'; 
			SessionProxy::SetValue('adminError', $array);
		}
		return $out;
	}

	/**
	 * Usuwa plik z pami�ci.
	 *
	 * @param <type> $name
	 * @return <type>
	 */
	public static function DeleteFileFromStorage($name,$localStorage = false) {
		if(STORAGE_TYPE == ST_TYPE_SERVER && $localStorage == false) {
			$logger = LoggerManager::getLogger(__CLASS__);
			$name = WP_API_EdyskLib::GetFileNameFromPath($name);
			$client = new WP_API_EdyskLib($name);
			$logger -> debug("File to delete: " . $name);
			try {
				$response = $client->DeleteFile();
			} catch (ApiException $e) {
				if (!$e->GetCode() == 404)
				{
					throw $e;
				}

				$response = false;
			}
			$logger->debug($client->GetLastResponse());
			return $response;		
		} else {
			return true;
		}
	}

	/**
	 * Usuwa plik z lokalnego dysku.
	 *
	 * @param <type> $file
	 * @param <type> $method
	 */
	public static function DeleteFile($file, $method) {
		$logger = LoggerManager::getLogger(__CLASS__);
		$fileName = end(explode('/', $file));
		$dirName = ereg_replace($fileName, "", $file);
		$success = exec("/opt/WP/bin/delete_file.sh ".$file." ".ereg_replace(PATH_ROOT.'/', '', $dirName));
		
		if($success==0) {
			$logger->info("Delete file: ".$fileName." to ".$dirName.", Method: ".$method.", Server reply: ".$success);
		} else {
			$logger->warn("Delete file: ".$fileName." to ".$dirName.", Method: ".$method." FAILED!".", Server reply: ".$success);
			$array = array();
			$array = SessionProxy::GetValue('adminError');
			$array[] = $fileName.' nie zosta� usuni�ty z jednego lub wi�cej swerwer�w!';
			SessionProxy::SetValue('adminError', $array);
		}
	}	

	/**
	 * Usuwa katalog.
	 *
	 * @param <type> $directory
	 * @param <type> $method
	 */
	public static function DeleteDirectory($directory, $method) {
		$logger = LoggerManager::getLogger(__CLASS__);
		$success = exec("/opt/WP/bin/delete_file.sh ".$directory."/ ".ereg_replace(PATH_ROOT.'/', '', $directory)."/");
		if($success==0) {
			$logger->info("Delete directory: ".$directory.", Method: ".$method.", Server reply: ".$success);
		} else {
			$logger->warn("Delete directory: ".$directory.", Method: ".$method." FAILED!".", Server reply: ".$success);
			$array = array();
			$array = SessionProxy::GetValue('adminError');
			$array[] = $directory.' nie zosta� usuni�ty z jednego lub wi�cej swerwer�w!';
			SessionProxy::SetValue('adminError', $array);
		}
	}	
}
?>